// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlexanderRendon

//@version=5
indicator("EMA 6/21/50 + MACD + AO + Panel + Botones + Alertas", overlay=true, shorttitle="EMAs 6/21/50 PRO - THE BUSINESS")


// === INPUTS GENERALES ===
start_year = input.int(defval=2025, title="Año de inicio")
longOnly   = input.bool(false, title="Solo señales en largo")

// === CONTROLES DE VISUALIZACIÓN ===
show_panel = input.bool(true,  title="Mostrar Panel Informativo")
show_emas  = input.bool(true,  title="Mostrar EMAs (6/21/50)")
show_tp_sl = input.bool(true,  title="Mostrar TP/SL Visual")

// === SL/TP en pips ===
sl_pips = input.int(200, minval=1, title="Stop Loss (pips)")
tp_pips = input.int(400, minval=1, title="Take Profit (pips)")

// Ajuste de pip según símbolo
pip     = syminfo.mintick * 10
sl_pts  = sl_pips * pip
tp_pts  = tp_pips * pip

// === EMAs ===
ema6  = ta.ema(hl2, 6)
ema21 = ta.ema(hl2, 21)
ema50 = ta.ema(close, 50)

// === MACD ===
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
macd_filter_long  = macdLine > signalLine
macd_filter_short = macdLine < signalLine

// === AO ===
ao      = ta.sma(hl2, 5) - ta.sma(hl2, 34)
ao_prev = ao[1]
ao_turn_down = ao < ao_prev and ao > 0
ao_turn_up   = ao > ao_prev and ao < 0

// === CONDICIONES ===
longCross  = ta.crossover(ema6, ema21)
shortCross = ta.crossunder(ema6, ema21)
in_time    = time > timestamp(start_year, 1, 1, 00, 00)

enterLong  = longCross and close > ema50 and macd_filter_long and not ao_turn_down and in_time
enterShort = shortCross and close < ema50 and macd_filter_short and not ao_turn_up and in_time and not longOnly
exitLong   = longOnly and shortCross

// === PLOT EMAs ===
plot(show_emas ? ema6  : na, "EMA 6",  color=color.orange)
plot(show_emas ? ema21 : na, "EMA 21", color=color.blue)
plot(show_emas ? ema50 : na, "EMA 50", color=color.gray)

// === SEÑALES VISUALES ===
plotshape(enterLong,  title="Entrada Long",  location=location.belowbar, color=color.lime, style=shape.triangleup,   size=size.small)
plotshape(enterShort, title="Entrada Short", location=location.abovebar, color=color.red,  style=shape.triangledown, size=size.small)
plotshape(exitLong,   title="Salida Long",   location=location.abovebar, color=color.black, style=shape.xcross,      size=size.tiny)

// === TP / SL VISUAL ===
plot(show_tp_sl and enterLong  ? close + tp_pts : na,  title="TP Long",  color=color.green, linewidth=1, style=plot.style_linebr)
plot(show_tp_sl and enterLong  ? close - sl_pts : na,  title="SL Long",  color=color.red,   linewidth=1, style=plot.style_linebr)
plot(show_tp_sl and enterShort ? close - tp_pts : na,  title="TP Short", color=color.green, linewidth=1, style=plot.style_linebr)
plot(show_tp_sl and enterShort ? close + sl_pts : na,  title="SL Short", color=color.red,   linewidth=1, style=plot.style_linebr)

// === PANEL INFORMATIVO ELEGANTE ===
// Colores elegantes (tonos suaves estilo dashboard)
colorBull = color.rgb(0, 200, 83, 80)     // Verde suave
colorBear = color.rgb(229, 57, 53, 80)    // Rojo suave
colorNeutral = color.rgb(96, 125, 139, 80)// Gris azulado

trend_color = close > ema50 ? colorBull : close < ema50 ? colorBear : colorNeutral
text_color  = color.white
trend_text  = close > ema50 ? "📈 Alcista" : close < ema50 ? "📉 Bajista" : "⚪ Neutra"

// Tiempo restante de vela
timeLeftMs = (time + timeframe.multiplier * 60000) - timenow
timeLeftSec = math.max(0, timeLeftMs / 1000)
mins = math.floor(timeLeftSec / 60)
secs = math.floor(timeLeftSec % 60)
time_str = str.format("{0}:{1,number,00}", mins, secs)

// TP y SL actuales (en precio)
tp_long_price  = close + tp_pts
sl_long_price  = close - sl_pts
tp_short_price = close - tp_pts
sl_short_price = close + sl_pts

// Creamos panel
var table infoPanel = table.new(position.top_right, 1, 1, border_width=2, frame_color=trend_color)

panel_text = "📊 PANEL INFORMATIVO\n" +
             "Tendencia: " + trend_text + "\n" +
             "Cruce EMAs: " + (longCross ? "🟢 Alcista" : shortCross ? "🔴 Bajista" : "⚪ Neutro") + "\n" +
             "MACD: " + (macd_filter_long ? "✅ Long" : macd_filter_short ? "✅ Short" : "❌ Neutro") + "\n" +
             "AO: " + (ao_turn_down ? "🔻 Débil" : ao_turn_up ? "🔺 Fuerte" : "🟡 Estable") + "\n" +
             "Señal Long: " + (enterLong ? "✅" : "❌") + "\n" +
             "Señal Short: " + (enterShort ? "✅" : "❌") + "\n" +
             "🎯 TP Long: " + (enterLong ? str.tostring(tp_long_price, format.mintick) : "-") + "\n" +
             "🛡 SL Long: " + (enterLong ? str.tostring(sl_long_price, format.mintick) : "-") + "\n" +
             "🎯 TP Short: " + (enterShort ? str.tostring(tp_short_price, format.mintick) : "-") + "\n" +
             "🛡 SL Short: " + (enterShort ? str.tostring(sl_short_price, format.mintick) : "-") + "\n" +
             "⏳ Tiempo vela: " + time_str

if show_panel
    table.cell(infoPanel, 0, 0, panel_text, text_color=text_color, bgcolor=trend_color, text_size=size.normal)
    table.set_frame_color(infoPanel, trend_color)
else
    table.cell(infoPanel, 0, 0, "", text_color=text_color, bgcolor=trend_color, text_size=size.normal)

// === ALERTAS ===
alertcondition(enterLong,  title="Entrada LONG",  message="Señal de compra detectada")
alertcondition(enterShort, title="Entrada SHORT", message="Señal de venta detectada")

// === ALERTA DINÁMICA ===
if enterLong
    alert("LONG;"  + syminfo.ticker + ";" +
          str.tostring(close, format.mintick) + ";" +
          str.tostring(tp_long_price, format.mintick) + ";" +
          str.tostring(sl_long_price, format.mintick),
          alert.freq_once_per_bar_close)

if enterShort
    alert("SHORT;" + syminfo.ticker + ";" +
          str.tostring(close, format.mintick) + ";" +
          str.tostring(tp_short_price, format.mintick) + ";" +
          str.tostring(sl_short_price, format.mintick),
          alert.freq_once_per_bar_close)

